package com.reverse.engineering.util.core.run.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import com.ApplicationProperties;
import com.reverse.engineering.util.core.run.bean.DatabaseProperties;
import com.reverse.engineering.util.core.run.bean.FileMessage;
import com.reverse.engineering.util.core.run.bean.SqlTemplates;
import com.reverse.engineering.util.core.run.bean.TemplateMessage;
import com.reverse.engineering.util.core.run.bean.Templates;
import com.reverse.engineering.util.core.run.bean.XmlTemplates;
import com.reverse.engineering.util.core.run.bean.java.File;
import com.reverse.engineering.util.core.run.bean.java.FileFeild;
import com.reverse.engineering.util.core.run.bean.java.FileProperties;
import com.reverse.engineering.util.core.run.constant.Constant;
import com.reverse.engineering.util.core.run.enums.FileGenerate;
import com.reverse.engineering.util.core.run.enums.JavaEquals;
import com.reverse.engineering.util.core.run.enums.Placeholder;
import com.reverse.engineering.util.core.run.enums.ReadType;

/**
 * 应用程序工具类
 * 
 * @author liu
 * @date 2020-08-14
 */
public class ApplicationUtils {

    /**
     * 创建文件
     * @throws IOException 
     */
    public static void createFile() throws IOException {
        createEntity();
        createDaoInterface();
        createDao();
        createServiceInterface();
        createService();
        createController();
        createXml();
        if (ApplicationProperties.read == ReadType.EXCEL) {
            createSql();
        }
    }

    /**
     * 创建Entity文件
     * @throws IOException 
     */
    public static void createEntity() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createEntity(filePropertie);
        }
    }

    /**
     * 创建Dao接口文件
     * @throws IOException 
     */
    public static void createDaoInterface() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createDaoInterface(filePropertie);
        }
    }

    /**
     * 创建Dao实现类文件
     * @throws IOException 
     */
    public static void createDao() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createDao(filePropertie);
        }
    }

    /**
     * 创建Service接口文件
     * @throws IOException 
     */
    public static void createServiceInterface() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createServiceInterface(filePropertie);
        }
    }

    /**
     * 创建Service实现类文件
     * @throws IOException 
     */
    public static void createService() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createService(filePropertie);
        }
    }

    /**
     * 创建Controller实现类文件
     * @throws IOException 
     */
    public static void createController() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createController(filePropertie);
        }
    }

    /**
     * 创建Xml文件
     * @throws IOException 
     */
    public static void createXml() throws IOException {
        for (FileProperties filePropertie : File.fileProperties) {
            createXml(filePropertie);
        }
    }

    /**
     * 创建Sql文件
     * @throws IOException 
     */
    public static void createSql() throws IOException {
        StringBuilder database = SqlTemplates.database;
        database.append(Templates.template.get(Templates.sqlSelectDatebase).getContent()
            .replace(Placeholder.DATABASE_NAME_D.getPlaceholder(), File.databaseNameD)).append("\n");
        database.append(Templates.template.get(Templates.sqlDropForeignKey).getContent()).append("\n\n");
        for (FileProperties filePropertie : File.fileProperties) {
            createSql(filePropertie);
        }

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(ApplicationProperties.sqlName, ".sql",
            FileGenerate.SQL.getGeneratePath(), database.toString());

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Entity文件
     * @throws IOException 
     */
    public static void createEntity(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        HashSet<String> asList = new HashSet<>();
        asList.addAll(Arrays.asList(templateMessageGetContent(Templates.entityPackage).split("\n")));
        if (filePropertie.getExistId()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.entityIdPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(Arrays.asList(templateMessageGetContent(Templates.entityIdDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistSerialNumber()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.entitySerialNumberPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(
                    Arrays.asList(templateMessageGetContent(Templates.entitySerialNumberDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistDel()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.entityDelPackage).split("\n")));
        }
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.entityFile);
        StringBuilder fields = new StringBuilder();
        StringBuilder methods = new StringBuilder();

        // 用属性替换模板文件占位符
        for (FileFeild fileFeild : filePropertie.getFileFeild()) {
            if (ApplicationProperties.id.equalsIgnoreCase(fileFeild.getFieldNameD())) {
                continue;
            }
            entityCreateFieldAndMethod(fields, methods, fileFeild, asList);
        }
        entityCreateIds(fields, methods, filePropertie);
        String packags = sortPackage(asList);

        // 把所有字段和方法替换掉文件模板
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGES.getPlaceholder(), packags);
        fileTemplate = fileTemplate.replace(Placeholder.FIELDS.getPlaceholder(), fields.toString());
        fileTemplate = fileTemplate.replace(Placeholder.METHODS.getPlaceholder(), methods.toString());
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getEntityClassNameD(), ".java",
            FileGenerate.ENTITY.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Dao接口文件
     * @throws IOException 
     */
    public static void createDaoInterface(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        HashSet<String> asList = new HashSet<>();
        asList.addAll(Arrays.asList(
            replacePlaceholder(templateMessageGetContent(Templates.daoInterfacePackage), filePropertie).split("\n")));
        if (filePropertie.getExistId()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoInterfaceIdPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoInterfaceIdDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistSerialNumber()) {
            asList.addAll(
                Arrays.asList(templateMessageGetContent(Templates.daoInterfaceSerialNumberPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(
                    Arrays.asList(templateMessageGetContent(Templates.daoInterfaceSerialNumberDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistDel()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoInterfaceDelPackage).split("\n")));
        }
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.daoInterfaceFile);

        // 用属性替换模板文件占位符
        fileTemplate = replaceBasePlaceholder(fileTemplate, filePropertie, asList, Templates.daoInterface);
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGES.getPlaceholder(), sortPackage(asList));
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getDaoInterfaceClassNameD(), ".java",
            FileGenerate.DAO_INTERFACE.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Dao实现类文件
     * @throws IOException 
     */
    public static void createDao(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        HashSet<String> asList = new HashSet<>();
        asList.addAll(Arrays
            .asList(replacePlaceholder(templateMessageGetContent(Templates.daoPackage), filePropertie).split("\n")));
        if (filePropertie.getExistId()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoIdPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoIdDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistSerialNumber()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoSerialNumberPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList
                    .addAll(Arrays.asList(templateMessageGetContent(Templates.daoSerialNumberDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistDel()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.daoDelPackage).split("\n")));
        }
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.daoFile);

        // 用属性替换模板文件占位符
        fileTemplate = replaceBasePlaceholder(fileTemplate, filePropertie, asList, Templates.dao);
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGES.getPlaceholder(), sortPackage(asList));
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getDaoImplClassNameD(), ".java",
            FileGenerate.DAO_IMPL.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Service接口文件
     * @throws IOException 
     */
    public static void createServiceInterface(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        HashSet<String> asList = new HashSet<>();
        asList.addAll(Arrays
            .asList(replacePlaceholder(templateMessageGetContent(Templates.serviceInterfacePackage), filePropertie)
                .split("\n")));
        if (filePropertie.getExistId()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.serviceInterfaceIdPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(
                    Arrays.asList(templateMessageGetContent(Templates.serviceInterfaceIdDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistSerialNumber()) {
            asList.addAll(
                Arrays.asList(templateMessageGetContent(Templates.serviceInterfaceSerialNumberPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(Arrays
                    .asList(templateMessageGetContent(Templates.serviceInterfaceSerialNumberDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistDel()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.serviceInterfaceDelPackage).split("\n")));
        }
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.serviceInterfaceFile);

        // 用属性替换模板文件占位符
        fileTemplate = replaceBasePlaceholder(fileTemplate, filePropertie, asList, Templates.serviceInterface);
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGES.getPlaceholder(), sortPackage(asList));
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getServiceInterfaceClassNameD(), ".java",
            FileGenerate.SERVICE_INTERFACE.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Service实现类文件
     * @throws IOException 
     */
    public static void createService(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        HashSet<String> asList = new HashSet<>();
        asList.addAll(Arrays.asList(
            replacePlaceholder(templateMessageGetContent(Templates.servicePackage), filePropertie).split("\n")));
        if (filePropertie.getExistId()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.serviceIdPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(Arrays.asList(templateMessageGetContent(Templates.serviceIdDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistSerialNumber()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.serviceSerialNumberPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(
                    Arrays.asList(templateMessageGetContent(Templates.serviceSerialNumberDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistDel()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.serviceDelPackage).split("\n")));
        }
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.serviceFile);

        // 用属性替换模板文件占位符
        fileTemplate = replaceBasePlaceholder(fileTemplate, filePropertie, asList, Templates.service);
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGES.getPlaceholder(), sortPackage(asList));
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getServiceImplClassNameD(), ".java",
            FileGenerate.SERVICE_IMPL.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Controller实现类文件
     * @throws IOException 
     */
    public static void createController(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        HashSet<String> asList = new HashSet<>();
        asList.addAll(Arrays.asList(
            replacePlaceholder(templateMessageGetContent(Templates.controllerPackage), filePropertie).split("\n")));
        if (filePropertie.getExistId()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.controllerIdPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(Arrays.asList(templateMessageGetContent(Templates.controllerIdDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistSerialNumber()) {
            asList
                .addAll(Arrays.asList(templateMessageGetContent(Templates.controllerSerialNumberPackage).split("\n")));
            if (filePropertie.getExistDel()) {
                asList.addAll(
                    Arrays.asList(templateMessageGetContent(Templates.controllerSerialNumberDelPackage).split("\n")));
            }
        }
        if (filePropertie.getExistDel()) {
            asList.addAll(Arrays.asList(templateMessageGetContent(Templates.controllerDelPackage).split("\n")));
        }
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.controllerFile);

        // 用属性替换模板文件占位符
        fileTemplate = replaceBasePlaceholder(fileTemplate, filePropertie, asList, Templates.controller);
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGES.getPlaceholder(), sortPackage(asList));
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getControllerImplClassNameD(), ".java",
            FileGenerate.CONTROLLER_IMPL.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Xml文件
     * @throws IOException 
     */
    public static void createXml(FileProperties filePropertie) throws IOException {
        // 读取模板文件
        String fileTemplate = templateMessageGetContent(Templates.xmlFile);
        XmlTemplates xmlTemplates = new XmlTemplates();

        // 用属性替换模板文件占位符
        fileTemplate = xmlReplaceBasePlaceholder(fileTemplate, filePropertie);
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);
        ArrayList<FileFeild> fileFeilds = filePropertie.getFileFeild();
        for (FileFeild fileFeild : fileFeilds) {
            xmlCreateField(fileFeild, filePropertie, xmlTemplates);
        }
        xmlTemplates.removeTwoLastChar();
        fileTemplate = fileTemplate.replace(Placeholder.RESULT_MAP.getPlaceholder(), xmlTemplates.getResultMap());
        fileTemplate = fileTemplate.replace(Placeholder.COLUMNS.getPlaceholder(), xmlTemplates.getColumns());
        fileTemplate = fileTemplate.replace(Placeholder.INSERT_VALUES.getPlaceholder(), xmlTemplates.getInsertValues());
        fileTemplate =
            fileTemplate.replace(Placeholder.INSERT_LIST_VALUES.getPlaceholder(), xmlTemplates.getInsertListValues());
        fileTemplate = fileTemplate.replace(Placeholder.UPDATE_VALUES.getPlaceholder(), xmlTemplates.getUpdateValues());
        fileTemplate =
            fileTemplate.replace(Placeholder.UPDATE_LIST_VALUES.getPlaceholder(), xmlTemplates.getUpdateListValues());
        fileTemplate = fileTemplate.replace(Placeholder.UPDATE_PARTIAL_VALUES.getPlaceholder(),
            xmlTemplates.getUpdatePartialValues());
        fileTemplate =
            fileTemplate.replace(Placeholder.WHERE_CONDITION.getPlaceholder(), xmlTemplates.getWhereCondition());
        fileTemplate =
            fileTemplate.replace(Placeholder.DELETE_CONDITION.getPlaceholder(), xmlTemplates.getDeleteCondition());
        fileTemplate = fileTemplate.replace(Placeholder.UPDATE_PARTIAL_LIST_VALUES.getPlaceholder(),
            xmlTemplates.getUpdatePartialListValues());
        fileTemplate = fileTemplate.replace(Placeholder.XING.getPlaceholder(), filePropertie.getXingD());

        // 封装文件信息类对象
        FileMessage fileMessage = new FileMessage(filePropertie.getDaoInterfaceClassNameD(), ".xml",
            FileGenerate.XML.getGeneratePath(), fileTemplate);

        // 创建文件
        FileUtils.createFileByString(fileMessage);
    }

    /**
     * 创建Sql文件
     * 
     * @param filePropertie
     * @throws IOException 
     */
    private static void createSql(FileProperties filePropertie) throws IOException {
        ArrayList<FileFeild> fileFeilds = filePropertie.getFileFeild();
        if (fileFeilds.size() == 0) {
            return;
        }

        // 读取模板文件
        TemplateMessage templateMessage = Templates.template.get(Templates.sqlFile);
        String fileTemplate = templateMessage.getContent();

        if (ApplicationProperties.dropTable) {
            fileTemplate = fileTemplate.replace(Placeholder.DROP_TABLE.getPlaceholder(), filePropertie.getTableNameD());
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.DROP_TABLE.getPlaceholder(), "");
        }
        // 用属性替换模板文件占位符
        fileTemplate = replacePlaceholder(fileTemplate, filePropertie);
        SqlTemplates sqlTemplates = new SqlTemplates();
        for (FileFeild fileFeild : fileFeilds) {
            sqlCreateField(fileFeild, filePropertie, sqlTemplates);
        }
        StringBuilder field = sqlTemplates.getField();
        StringBuilder foreignKey = sqlTemplates.getForeignKey();
        field.append(foreignKey);
        fileTemplate = fileTemplate.replace(Placeholder.FIELDS.getPlaceholder(),
            field.replace(field.length() - 2, field.length(), ""));

        if (filePropertie.getExistId() && filePropertie.getExistAutoIncrement()) {
            fileTemplate = fileTemplate.replace(Placeholder.AUTO_INCREMENT.getPlaceholder(),
                DatabaseProperties.getAutoIncrementSql() + " ");
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.AUTO_INCREMENT.getPlaceholder(), "");
        }
        fileTemplate = fileTemplate.replace(Placeholder.DATABASE_COLLATE.getPlaceholder(), File.databaseCollate);
        fileTemplate = fileTemplate.replace(Placeholder.DATABASE_CHARACTER.getPlaceholder(), File.databaseCharacter);
        fileTemplate =
            fileTemplate.replace(Placeholder.CLASS_COMMENT.getPlaceholder(), filePropertie.getClassComment());
        fileTemplate += "\n\n";

        SqlTemplates.database.append(fileTemplate);
    }

    private static void sqlCreateField(FileFeild fileFeild, FileProperties filePropertie, SqlTemplates sqlTemplates) {
        StringBuilder field = sqlTemplates.getField();
        // 读取模板文件
        TemplateMessage sqlField = Templates.template.get(Templates.sqlField);
        String sqlFieldContent = sqlField.getContent();
        sqlFieldContent = sqlFieldContent.replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(),
            fileFeild.getFieldNameDatabaseD());
        sqlFieldContent = sqlFieldContent.replace(Placeholder.DATABASE_FIELD_TYPE_D.getPlaceholder(),
            fileFeild.getFieldDatabaseType().toUpperCase());
        if (fileFeild.getFieldLength() != null) {
            sqlFieldContent = sqlFieldContent.replace(Placeholder.DATABASE_FIELD_LENGTH.getPlaceholder(),
                "(" + fileFeild.getFieldLength() + ")");
        } else {
            sqlFieldContent = sqlFieldContent.replace(Placeholder.DATABASE_FIELD_LENGTH.getPlaceholder(), "");
        }
        String[] fieldPropertys = fileFeild.getFieldProperty();
        if (fieldPropertys != null) {
            String fieldProperty = "";
            for (String string : fieldPropertys) {
                fieldProperty += string += " ";
            }
            sqlFieldContent =
                sqlFieldContent.replace(Placeholder.DATABASE_FIELD_PROPERTIES.getPlaceholder(), fieldProperty);
        } else {
            sqlFieldContent = sqlFieldContent.replace(Placeholder.DATABASE_FIELD_PROPERTIES.getPlaceholder(), "");
        }
        sqlFieldContent =
            sqlFieldContent.replace(Placeholder.FIELD_COMMENT.getPlaceholder(), fileFeild.getFieldComment());
        field.append(sqlFieldContent).append("\n");

        String referencesField = fileFeild.getReferencesField();
        String referencesTable = fileFeild.getReferencesTable();
        String referencesComment = fileFeild.getReferencesComment();
        if (referencesField != null && !"".equals(referencesField.trim()) && referencesTable != null
            && !"".equals(referencesTable.trim()) && referencesComment != null
            && !"".equals(referencesComment.trim())) {
            StringBuilder foreignKey = sqlTemplates.getForeignKey();
            // 读取模板文件
            TemplateMessage sqlForeignKey = Templates.template.get(Templates.sqlForeignKey);
            String sqlForeignKeyContent = sqlForeignKey.getContent();
            sqlForeignKeyContent =
                sqlForeignKeyContent.replace(Placeholder.FOREIGN_KEY_NAME.getPlaceholder(), referencesComment);
            sqlForeignKeyContent =
                sqlForeignKeyContent.replace(Placeholder.FOREIGN_KEY_TABLE_NAME.getPlaceholder(), referencesTable);
            sqlForeignKeyContent = sqlForeignKeyContent
                .replace(Placeholder.FOREIGN_KEY_TABLE_FIELD_NAME.getPlaceholder(), referencesField);
            sqlForeignKeyContent = sqlForeignKeyContent.replace(Placeholder.FIELD_NAME_D.getPlaceholder(),
                fileFeild.getFieldNameDatabaseD());
            foreignKey.append(sqlForeignKeyContent).append("\n");
        }
    }

    /**
     * 替换模板占位符并拼接
     * @param filePropertie 
     * 
     * @param filePropertie
     * @param xmlTemplates
     * @throws IOException
     */
    public static void xmlCreateField(FileFeild fileFeild, FileProperties filePropertie, XmlTemplates xmlTemplates)
        throws IOException {
        String dto_ = "dto.";
        // 读取模板文件
        StringBuilder resultMap = xmlTemplates.getResultMap();
        String xmlResultMapTemplate = Templates.template.get(Templates.xmlResultMap).getContent();
        if (resultMap.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlResultMapTemplate = xmlResultMapTemplate.replace("<result", "<id");
                xmlResultMapTemplate = xmlResultMapTemplate.replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(),
                    filePropertie.getDatabaseIdNameD());
                xmlResultMapTemplate = xmlResultMapTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                    filePropertie.getJavaIdNameX());
                xmlResultMapTemplate = xmlResultMapTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlResultMapTemplate = xmlResultMapTemplate.replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(),
                    fileFeild.getFieldNameDatabaseD());
                xmlResultMapTemplate =
                    xmlResultMapTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
                xmlResultMapTemplate = xmlResultMapTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlResultMapTemplate = xmlResultMapTemplate.replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(),
                fileFeild.getFieldNameDatabaseD());
            xmlResultMapTemplate =
                xmlResultMapTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlResultMapTemplate = xmlResultMapTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        resultMap.append(xmlResultMapTemplate).append("\n");

        StringBuilder columns = xmlTemplates.getColumns();
        String xmlColumnsTemplate = Templates.template.get(Templates.xmlColumns).getContent();
        if (columns.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlColumnsTemplate = xmlColumnsTemplate.replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(),
                    filePropertie.getDatabaseIdNameD());
            } else {
                xmlColumnsTemplate = xmlColumnsTemplate.replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(),
                    fileFeild.getFieldNameDatabaseD());
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            int num = columns.length() - (columns.lastIndexOf("\n") + 2);
            if (num > 110) {
                String empty = "";
                for (int i = 0; i < xmlColumnsTemplate.length(); i++) {
                    if (' ' == xmlColumnsTemplate.charAt(i)) {
                        empty += " ";
                    } else {
                        break;
                    }
                }
                columns.replace(columns.length() - 1, columns.length(), "").append("\n" + empty);
            }
            xmlColumnsTemplate = xmlColumnsTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD()).trim();
        }
        columns.append(xmlColumnsTemplate).append(" ");

        StringBuilder insertValues = xmlTemplates.getInsertValues();
        String xmlInsertValuesTemplate = Templates.template.get(Templates.xmlInsertValues).getContent();
        String empty = "";
        for (int i = 0; i < xmlInsertValuesTemplate.length(); i++) {
            if (' ' == xmlInsertValuesTemplate.charAt(i)) {
                empty += " ";
            } else {
                break;
            }
        }
        if (insertValues.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlInsertValuesTemplate = empty + "null,";
            } else {
                xmlInsertValuesTemplate = xmlInsertValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
                xmlInsertValuesTemplate = xmlInsertValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                    fileFeild.getFieldNameX());
                xmlInsertValuesTemplate = xmlInsertValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlInsertValuesTemplate = xmlInsertValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
            xmlInsertValuesTemplate =
                xmlInsertValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlInsertValuesTemplate = xmlInsertValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        insertValues.append(xmlInsertValuesTemplate).append("\n");

        StringBuilder insertListValues = xmlTemplates.getInsertListValues();
        String xmlInsertListValuesTemplate = Templates.template.get(Templates.xmlInsertValues).getContent();
        if (insertListValues.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlInsertListValuesTemplate = empty + "null,";
            } else {
                xmlInsertListValuesTemplate =
                    xmlInsertListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
                xmlInsertListValuesTemplate = xmlInsertListValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
                xmlInsertListValuesTemplate = xmlInsertListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlInsertListValuesTemplate = xmlInsertListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
            xmlInsertListValuesTemplate = xmlInsertListValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlInsertListValuesTemplate = xmlInsertListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        insertListValues.append(xmlInsertListValuesTemplate).append("\n");

        StringBuilder updateValues = xmlTemplates.getUpdateValues();
        String xmlUpdateValuesTemplate = Templates.template.get(Templates.xmlUpdateValues).getContent();
        if (updateValues.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), filePropertie.getDatabaseIdNameD());
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                    filePropertie.getJavaIdNameX());
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                    fileFeild.getFieldNameX());
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlUpdateValuesTemplate = xmlUpdateValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
            xmlUpdateValuesTemplate = xmlUpdateValuesTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
            xmlUpdateValuesTemplate =
                xmlUpdateValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlUpdateValuesTemplate = xmlUpdateValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        updateValues.append(xmlUpdateValuesTemplate).append("\n");

        StringBuilder updateListValues = xmlTemplates.getUpdateListValues();
        String xmlUpdateListValuesTemplate = Templates.template.get(Templates.xmlUpdateValues).getContent();
        if (updateListValues.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlUpdateListValuesTemplate =
                    xmlUpdateListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
                xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), filePropertie.getDatabaseIdNameD());
                xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), filePropertie.getJavaIdNameX());
                xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlUpdateListValuesTemplate =
                    xmlUpdateListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
                xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
                xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
                if (fileFeild.getFieldXmlType() != null) {
                    xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                        .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
                }
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
            xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
            xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlUpdateListValuesTemplate = xmlUpdateListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        updateListValues.append(xmlUpdateListValuesTemplate).append("\n");

        StringBuilder updatePartialValues = xmlTemplates.getUpdatePartialValues();
        String xmlUpdatePartialValuesTemplate = Templates.template.get(Templates.xmlUpdatePartialValues).getContent();
        if (updatePartialValues.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlUpdatePartialValuesTemplate =
                    xmlUpdatePartialValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
                xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), filePropertie.getDatabaseIdNameD());
                xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), filePropertie.getJavaIdNameX());
                xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlUpdatePartialValuesTemplate =
                    xmlUpdatePartialValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
                xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
                xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
                if (fileFeild.getFieldXmlType() != null) {
                    xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                        .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
                }
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlUpdatePartialValuesTemplate =
                xmlUpdatePartialValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), "");
            xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
            xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlUpdatePartialValuesTemplate = xmlUpdatePartialValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        updatePartialValues.append(xmlUpdatePartialValuesTemplate).append("\n");

        StringBuilder updatePartialListValues = xmlTemplates.getUpdatePartialListValues();
        String xmlUpdatePartialListValuesTemplate =
            Templates.template.get(Templates.xmlUpdatePartialValues).getContent();
        if (updatePartialListValues.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlUpdatePartialListValuesTemplate =
                    xmlUpdatePartialListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
                xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), filePropertie.getDatabaseIdNameD());
                xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), filePropertie.getJavaIdNameX());
                xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlUpdatePartialListValuesTemplate =
                    xmlUpdatePartialListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
                xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
                xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
                if (fileFeild.getFieldXmlType() != null) {
                    xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                        .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
                }
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlUpdatePartialListValuesTemplate =
                xmlUpdatePartialListValuesTemplate.replace(Placeholder.DTO_.getPlaceholder(), dto_);
            xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
            xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlUpdatePartialListValuesTemplate = xmlUpdatePartialListValuesTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        updatePartialListValues.append(xmlUpdatePartialListValuesTemplate).append("\n");

        StringBuilder whereCondition = xmlTemplates.getWhereCondition();
        String xmlWhereConditionTemplate = Templates.template.get(Templates.xmlWhereCondition).getContent();
        if (whereCondition.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlWhereConditionTemplate = xmlWhereConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), filePropertie.getDatabaseIdNameD());
                xmlWhereConditionTemplate = xmlWhereConditionTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                    filePropertie.getJavaIdNameX());
                xmlWhereConditionTemplate = xmlWhereConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlWhereConditionTemplate = xmlWhereConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
                xmlWhereConditionTemplate = xmlWhereConditionTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                    fileFeild.getFieldNameX());
                if (fileFeild.getFieldXmlType() != null) {
                    xmlWhereConditionTemplate = xmlWhereConditionTemplate
                        .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
                }
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlWhereConditionTemplate = xmlWhereConditionTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
            xmlWhereConditionTemplate =
                xmlWhereConditionTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlWhereConditionTemplate = xmlWhereConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        whereCondition.append(xmlWhereConditionTemplate).append("\n");

        StringBuilder deleteCondition = xmlTemplates.getDeleteCondition();
        String xmlDeleteConditionTemplate = Templates.template.get(Templates.xmlDeleteCondition).getContent();
        if (deleteCondition.length() == 0) {
            if (filePropertie.getExistId()) {
                xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), filePropertie.getDatabaseIdNameD());
                xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), filePropertie.getJavaIdNameX());
                xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            } else {
                xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
                xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                    .replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());
                if (fileFeild.getFieldXmlType() != null) {
                    xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                        .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
                }
            }
        } else if (filePropertie.getDatabaseIdName() == null
            || !filePropertie.getDatabaseIdName().equals(fileFeild.getFieldNameDatabase())) {
            xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                .replace(Placeholder.DATABASE_FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameDatabaseD());
            xmlDeleteConditionTemplate = xmlDeleteConditionTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(),
                fileFeild.getFieldNameX());
            if (fileFeild.getFieldXmlType() != null) {
                xmlDeleteConditionTemplate = xmlDeleteConditionTemplate
                    .replace(Placeholder.DATABASE_FIELD_XML_TYPE.getPlaceholder(), fileFeild.getFieldXmlType());
            }
        }
        deleteCondition.append(xmlDeleteConditionTemplate).append("\n");
    }

    /**
     * 对包排序
     * 
     * @param set
     * @return
     */
    public static String sortPackage(HashSet<String> set) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder other = new StringBuilder();
        ArrayList<String> otherList = new ArrayList<>();
        for (String string : set) {
            // 标记是否在集合中
            boolean is = false;
            for (String sort : ApplicationProperties.importSort) {
                if (string.substring(0, string.indexOf(".") >= 0 ? string.indexOf(".") : string.length())
                    .equals(sort)) {
                    is = true;
                    break;
                }
            }
            if (!is && !"".equals(string.trim())) {
                otherList.add(string);
            }
        }
        if (otherList.size() > 0) {
            other.append(sortPackage(otherList)).append("\n");
            if (ApplicationProperties.importSortOther) {
                stringBuilder.append(other);
                if (ApplicationProperties.importSortSpacing) {
                    stringBuilder.append("\n");
                }
            }
        }
        // 指定的包添加并排序
        for (String sort : ApplicationProperties.importSort) {
            ArrayList<String> list = new ArrayList<>();
            for (String string : set) {
                if (string.substring(0, string.indexOf(".") >= 0 ? string.indexOf(".") : string.length())
                    .equals(sort)) {
                    list.add(string);
                }
            }
            stringBuilder.append(sortPackage(list));
            if (list.size() > 0) {
                stringBuilder.append("\n");
                if (ApplicationProperties.importSortSpacing) {
                    stringBuilder.append("\n");
                }
            }
        }
        if (otherList.size() > 0) {
            if (!ApplicationProperties.importSortOther) {
                stringBuilder.append(other);
                if (ApplicationProperties.importSortSpacing) {
                    stringBuilder.append("\n");
                }
            }
        }
        stringBuilder.replace(stringBuilder.lastIndexOf("\n"), stringBuilder.length(), "");
        if (ApplicationProperties.importSortSpacing) {
            stringBuilder.replace(stringBuilder.lastIndexOf("\n"), stringBuilder.length(), "");
        }
        return stringBuilder.toString();
    }

    /**
     * 对包排序
     * 
     * @param list
     * @return
     */
    public static String sortPackage(ArrayList<String> list) {
        if (list.size() < 1) {
            return "";
        }
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - i - 1; j++) {
                if (list.get(j).compareTo(list.get(j + 1)) > 0) {
                    String strI = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, strI);
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : list) {
            stringBuilder.append("import " + string + ";\n");
        }
        stringBuilder.replace(stringBuilder.lastIndexOf("\n"), stringBuilder.length(), "");
        return stringBuilder.toString();
    }

    /**
     * 替换模板占位符并拼接
     * 
     * @param fields
     * @param methods
     * @param fileFeild
     * @param asList 
     * @param filePropertie
     */
    public static void entityCreateFieldAndMethod(StringBuilder fields, StringBuilder methods, FileFeild fileFeild,
        HashSet<String> asList) {

        asList.add(fileFeild.getFieldEquals().getPackageName());
        fields.append("\n")
            .append(replacePropertyPlaceholder(templateMessageGetContent(Templates.entityField), fileFeild));
        methods.append(replacePropertyPlaceholder(templateMessageGetContent(Templates.entityMethod), fileFeild))
            .append("\n");

    }

    /**
     * 替换模板占位符并拼接(特殊字段,如ID)
     * 
     * @param fields
     * @param methods
     * @param fileFeild
     * @param filePropertie
     */
    public static void entityCreateIds(StringBuilder fields, StringBuilder methods, FileProperties fileProperties) {
        if (fileProperties.getExistId()) {
            String fieldComment = fileProperties.getJavaIdComment();
            fieldComment = fieldComment != null ? fileProperties.getJavaIdComment() + "集合" : "";
            String fieldNameD = fileProperties.getJavaIdNameD() + "s";
            String fieldNameX = fileProperties.getJavaIdNameX() + "s";
            String fieldType = "List<" + fileProperties.getJavaIdType() + ">";
            String fieldTemplate = Templates.template.get(Templates.entityField).getContent();
            fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_COMMENT.getPlaceholder(), fieldComment);
            fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_TYPE.getPlaceholder(), fieldType);
            fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fieldNameX);
            fields.append("\n").append(fieldTemplate);

            String methodTemplate = Templates.template.get(Templates.entityMethod).getContent();
            if (fieldComment != null) {
                methodTemplate = methodTemplate.replace(Placeholder.FIELD_COMMENT.getPlaceholder(), fieldComment);
            }
            methodTemplate = methodTemplate.replace(Placeholder.FIELD_TYPE.getPlaceholder(), fieldType);
            methodTemplate = methodTemplate.replace(Placeholder.FIELD_NAME_D.getPlaceholder(), fieldNameD);
            methodTemplate = methodTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fieldNameX);
            methods.append(methodTemplate).append("\n");
        }

        if (fileProperties.getExistSerialNumber()) {
            String fieldComment = fileProperties.getJavaSerialNumberComment();
            fieldComment = fieldComment != null ? fileProperties.getJavaSerialNumberComment() + "集合" : "";
            String fieldNameD = fileProperties.getJavaSerialNumberNameD() + "s";
            String fieldNameX = fileProperties.getJavaSerialNumberNameX() + "s";
            String fieldType = "List<" + fileProperties.getJavaSerialNumberType() + ">";
            String fieldTemplate = Templates.template.get(Templates.entityField).getContent();
            fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_COMMENT.getPlaceholder(), fieldComment);
            fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_TYPE.getPlaceholder(), fieldType);
            fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fieldNameX);
            fields.append("\n").append(fieldTemplate);

            String methodTemplate = Templates.template.get(Templates.entityMethod).getContent();
            if (fieldComment != null) {
                methodTemplate = methodTemplate.replace(Placeholder.FIELD_COMMENT.getPlaceholder(), fieldComment);
            }
            methodTemplate = methodTemplate.replace(Placeholder.FIELD_TYPE.getPlaceholder(), fieldType);
            methodTemplate = methodTemplate.replace(Placeholder.FIELD_NAME_D.getPlaceholder(), fieldNameD);
            methodTemplate = methodTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fieldNameX);
            methods.append(methodTemplate).append("\n");
        }

    }

    /**
     * 从模板获取内容
     * 
     * @param template
     * @return
     */
    public static String templateMessageGetContent(String template) {
        TemplateMessage templateMessage = Templates.template.get(template);
        return templateMessage.getContent();
    }

    /**
     * 替换属性占位符
     * 
     * @param fileTemplate
     * @param filePropertie
     * @return
     */
    public static String replacePlaceholder(String fileTemplate, FileProperties filePropertie) {
        fileTemplate = fileTemplate.replace(Placeholder.BASEPATHX.getPlaceholder(), File.basepathX);
        fileTemplate = fileTemplate.replace(Placeholder.BASEPATHD.getPlaceholder(), File.basepathD);
        fileTemplate = fileTemplate.replace(Placeholder.BASEPATHXX.getPlaceholder(), File.basepathXX);
        fileTemplate = fileTemplate.replace(Placeholder.BASEPATHDD.getPlaceholder(), File.basepathDD);
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGE_NAME_ENTITY.getPlaceholder(),
            FileGenerate.ENTITY.getPackageName());
        fileTemplate =
            fileTemplate.replace(Placeholder.PACKAGE_NAME_DAO.getPlaceholder(), FileGenerate.DAO_IMPL.getPackageName());
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGE_NAME_DAO_INTERFACE.getPlaceholder(),
            FileGenerate.DAO_INTERFACE.getPackageName());
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGE_NAME_SERVICE.getPlaceholder(),
            FileGenerate.SERVICE_IMPL.getPackageName());
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGE_NAME_SERVICE_INTERFACE.getPlaceholder(),
            FileGenerate.SERVICE_INTERFACE.getPackageName());
        fileTemplate = fileTemplate.replace(Placeholder.PACKAGE_NAME_CONTROLLER.getPlaceholder(),
            FileGenerate.CONTROLLER_IMPL.getPackageName());
        fileTemplate =
            fileTemplate.replace(Placeholder.ENTITY_CLASS_NAME_D.getPlaceholder(), filePropertie.getEntityClassNameD());
        fileTemplate =
            fileTemplate.replace(Placeholder.ENTITY_CLASS_NAME_X.getPlaceholder(), filePropertie.getEntityClassNameX());
        fileTemplate =
            fileTemplate.replace(Placeholder.ENTITY_PARAM_NAME_D.getPlaceholder(), filePropertie.getEntityParamNameD());
        fileTemplate =
            fileTemplate.replace(Placeholder.ENTITY_PARAM_NAME_X.getPlaceholder(), filePropertie.getEntityParamNameX());
        fileTemplate =
            fileTemplate.replace(Placeholder.ENTITY_BEAN_NAME.getPlaceholder(), filePropertie.getEntityBeanName());
        fileTemplate = fileTemplate.replace(Placeholder.DAO_INTERFACE_CLASS_NAME_D.getPlaceholder(),
            filePropertie.getDaoInterfaceClassNameD());
        fileTemplate = fileTemplate.replace(Placeholder.DAO_INTERFACE_CLASS_NAME_X.getPlaceholder(),
            filePropertie.getDaoInterfaceClassNameX());
        fileTemplate = fileTemplate.replace(Placeholder.DAO_INTERFACE_PARAM_NAME_D.getPlaceholder(),
            filePropertie.getDaoInterfaceParamNameD());
        fileTemplate = fileTemplate.replace(Placeholder.DAO_INTERFACE_PARAM_NAME_X.getPlaceholder(),
            filePropertie.getDaoInterfaceParamNameX());
        fileTemplate = fileTemplate.replace(Placeholder.DAO_INTERFACE_BEAN_NAME.getPlaceholder(),
            filePropertie.getDaoInterfaceBeanName());
        fileTemplate =
            fileTemplate.replace(Placeholder.DAO_CLASS_NAME_D.getPlaceholder(), filePropertie.getDaoImplClassNameD());
        fileTemplate =
            fileTemplate.replace(Placeholder.DAO_CLASS_NAME_X.getPlaceholder(), filePropertie.getDaoImplClassNameX());
        fileTemplate =
            fileTemplate.replace(Placeholder.DAO_PARAM_NAME_D.getPlaceholder(), filePropertie.getDaoImplParamNameD());
        fileTemplate =
            fileTemplate.replace(Placeholder.DAO_PARAM_NAME_X.getPlaceholder(), filePropertie.getDaoImplParamNameX());
        fileTemplate =
            fileTemplate.replace(Placeholder.DAO_BEAN_NAME.getPlaceholder(), filePropertie.getDaoImplBeanName());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_INTERFACE_CLASS_NAME_D.getPlaceholder(),
            filePropertie.getServiceInterfaceClassNameD());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_INTERFACE_CLASS_NAME_X.getPlaceholder(),
            filePropertie.getServiceInterfaceClassNameX());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_INTERFACE_PARAM_NAME_D.getPlaceholder(),
            filePropertie.getServiceInterfaceParamNameD());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_INTERFACE_PARAM_NAME_X.getPlaceholder(),
            filePropertie.getServiceInterfaceParamNameX());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_INTERFACE_BEAN_NAME.getPlaceholder(),
            filePropertie.getServiceInterfaceBeanName());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_CLASS_NAME_D.getPlaceholder(),
            filePropertie.getServiceImplClassNameD());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_CLASS_NAME_X.getPlaceholder(),
            filePropertie.getServiceImplClassNameX());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_PARAM_NAME_D.getPlaceholder(),
            filePropertie.getServiceImplParamNameD());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_PARAM_NAME_X.getPlaceholder(),
            filePropertie.getServiceImplParamNameX());
        fileTemplate = fileTemplate.replace(Placeholder.SERVICE_BEAN_NAME.getPlaceholder(),
            filePropertie.getServiceImplBeanName());
        fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_CLASS_NAME_D.getPlaceholder(),
            filePropertie.getControllerImplClassNameD());
        fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_CLASS_NAME_X.getPlaceholder(),
            filePropertie.getControllerImplClassNameX());
        fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_PARAM_NAME_D.getPlaceholder(),
            filePropertie.getControllerImplParamNameD());
        fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_PARAM_NAME_X.getPlaceholder(),
            filePropertie.getControllerImplParamNameX());
        fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_BEAN_NAME.getPlaceholder(),
            filePropertie.getControllerImplBeanName());

        fileTemplate =
            fileTemplate.replace(Placeholder.CLASS_COMMENT.getPlaceholder(), filePropertie.getClassComment());
        fileTemplate =
            fileTemplate.replace(Placeholder.COMMENT_DATE.getPlaceholder(), filePropertie.getClassCommentDate());
        fileTemplate =
            fileTemplate.replace(Placeholder.SERIAL_VERSION_UID.getPlaceholder(), filePropertie.getSerialVersionUID());

        fileTemplate =
            fileTemplate.replace(Placeholder.DATABASE_TABLE_NAME_Y.getPlaceholder(), filePropertie.getTableName());
        fileTemplate =
            fileTemplate.replace(Placeholder.DATABASE_TABLE_NAME_D.getPlaceholder(), filePropertie.getTableNameD());
        fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_IS_DEL_NUMBER.getPlaceholder(),
            ApplicationProperties.DEL_NUMBER + "");
        fileTemplate =
            fileTemplate.replace(Placeholder.CONTROLLER_URL.getPlaceholder(), filePropertie.getControllerMapping());
        if (StringUtils.isNotBlank(ApplicationProperties.idXmlParam)) {
            fileTemplate =
                fileTemplate.replace(Placeholder.ID_XML_PARAM.getPlaceholder(), ApplicationProperties.idXmlParam);
        } else {
            fileTemplate =
                fileTemplate.replace(Placeholder.ID_XML_PARAM.getPlaceholder(), filePropertie.getXmlIdParamType());
        }
        if (ApplicationProperties.controllerMappingSuffix) {
            fileTemplate =
                fileTemplate.replace(Placeholder.CONTROLLER_SUFFIX_ACTION.getPlaceholder(), Constant._action);
            fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_SUFFIX_SHOW.getPlaceholder(), Constant._show);
            fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_SUFFIX_DO.getPlaceholder(), Constant._do);
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_SUFFIX_ACTION.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_SUFFIX_SHOW.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.CONTROLLER_SUFFIX_DO.getPlaceholder(), "");
        }
        if (filePropertie.getExistId()) {
            fileTemplate =
                fileTemplate.replace(Placeholder.JDBC_ID_TYPE.getPlaceholder(), filePropertie.getXmlIdType());
            fileTemplate = fileTemplate.replace(Placeholder.ID_D.getPlaceholder(), filePropertie.getJavaIdNameD());
            fileTemplate = fileTemplate.replace(Placeholder.ID_X.getPlaceholder(), filePropertie.getJavaIdNameX());
            fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_ID_Y.getPlaceholder(),
                filePropertie.getDatabaseIdName());
            fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_ID_D.getPlaceholder(),
                filePropertie.getDatabaseIdNameD());
            fileTemplate =
                fileTemplate.replace(Placeholder.XML_ID_PARAM_TYPE.getPlaceholder(), filePropertie.getXmlIdParamType());
            fileTemplate =
                fileTemplate.replace(Placeholder.ID_JAVA_TYPE.getPlaceholder(), filePropertie.getJavaIdType());
        }
        if (filePropertie.getExistSerialNumber()) {
            fileTemplate = fileTemplate.replace(Placeholder.JDBC_SERIAL_NUMBER_TYPE.getPlaceholder(),
                filePropertie.getXmlSerialNumberType());
            fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_D.getPlaceholder(),
                filePropertie.getJavaSerialNumberNameD());
            fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_X.getPlaceholder(),
                filePropertie.getJavaSerialNumberNameX());
            fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_SERIAL_NUMBER_Y.getPlaceholder(),
                filePropertie.getDatabaseSerialNumberName());
            fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_SERIAL_NUMBER_D.getPlaceholder(),
                filePropertie.getDatabaseSerialNumberNameD());
            fileTemplate = fileTemplate.replace(Placeholder.XML_SERIAL_NUMBER_PARAM_TYPE.getPlaceholder(),
                filePropertie.getXmlSerialNumberParamType());
            fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_JAVA_TYPE.getPlaceholder(),
                filePropertie.getJavaSerialNumberType());
        }
        if (filePropertie.getExistDel()) {
            fileTemplate =
                fileTemplate.replace(Placeholder.STATUS_NO_LIST.getPlaceholder(), filePropertie.getDelNumberList());
            fileTemplate =
                fileTemplate.replace(Placeholder.JDBC_DEL_TYPE.getPlaceholder(), filePropertie.getXmlDelType());
            fileTemplate = fileTemplate.replace(Placeholder.DEL_D.getPlaceholder(), filePropertie.getJavaDelNameD());
            fileTemplate = fileTemplate.replace(Placeholder.DEL_X.getPlaceholder(), filePropertie.getJavaDelNameX());
            fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_IS_DEL_Y.getPlaceholder(),
                filePropertie.getDatabaseDelName());
            fileTemplate = fileTemplate.replace(Placeholder.DATABASE_FIELD_IS_DEL_D.getPlaceholder(),
                filePropertie.getDatabaseDelNameD());
            fileTemplate = fileTemplate.replace(Placeholder.XML_DEL_PARAM_TYPE.getPlaceholder(),
                filePropertie.getXmlDelParamType());
            fileTemplate =
                fileTemplate.replace(Placeholder.DEL_JAVA_TYPE.getPlaceholder(), filePropertie.getJavaDelType());
            fileTemplate = fileTemplate.replace(Placeholder.STATUS_NO.getPlaceholder(),
                ApplicationProperties.findIsDelTypeTrueFieldName);
            fileTemplate = fileTemplate.replace(Placeholder.STATUS_NO_XML_TYPE.getPlaceholder(),
                ApplicationProperties.findIsDelTypeTrueFieldType.getXml());
        }

        return fileTemplate;
    }

    /**
     * 给模板替换占位符
     * 
     * @param fileTemplate
     * @param filePropertie
     * @return
     */
    public static String xmlReplaceBasePlaceholder(String fileTemplate, FileProperties filePropertie) {
        fileTemplate = fileTemplate.replace(Placeholder.INSERT_ID.getPlaceholder(),
            "\n" + Templates.template.get(Templates.xmlInsertInsertId).getContent());
        if (filePropertie.getExistId()) {
            fileTemplate = fileTemplate.replace(Placeholder.INSERT_ID.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlInsertInsertId).getContent());
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_ID.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlDeleteId).getContent() + "\n");
            if (filePropertie.getExistDel()) {
                fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE_ID.getPlaceholder(),
                    "\n" + Templates.template.get(Templates.xmlDeleteUpdateId).getContent() + "\n");
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE_ID.getPlaceholder(), "");
            }
            fileTemplate = fileTemplate.replace(Placeholder.SELECT_ID.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlSelectId).getContent() + "\n");
            fileTemplate = fileTemplate.replace(Placeholder.UPDATE_ID.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlUpdateId).getContent() + "\n");
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.INSERT_ID.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_ID.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE_ID.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.SELECT_ID.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.UPDATE_ID.getPlaceholder(), "");
        }
        if (filePropertie.getExistSerialNumber()) {
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_SERIAL_NUMBER.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlDeleteSerialNumber).getContent() + "\n");
            if (filePropertie.getExistDel()) {
                fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE_SERIAL_NUMBER.getPlaceholder(),
                    "\n" + Templates.template.get(Templates.xmlDeleteUpdateSerialNumber).getContent() + "\n");
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE_SERIAL_NUMBER.getPlaceholder(), "");
            }
            fileTemplate = fileTemplate.replace(Placeholder.SELECT_SERIAL_NUMBER.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlSelectSerialNumber).getContent() + "\n");
            fileTemplate = fileTemplate.replace(Placeholder.UPDATE_SERIAL_NUMBER.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlUpdateSelectSerialNumber).getContent() + "\n");
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_SERIAL_NUMBER.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE_SERIAL_NUMBER.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.SELECT_SERIAL_NUMBER.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.UPDATE_SERIAL_NUMBER.getPlaceholder(), "");
        }
        if (filePropertie.getExistDel()) {
            if (ApplicationProperties.findIsDel) {
                if (ApplicationProperties.findIsDelType) {
                    fileTemplate = fileTemplate.replace(Placeholder.FIND_IS_DEL.getPlaceholder(),
                        "\n" + Templates.template.get(Templates.xmlFindIsDelByBase).getContent());
                } else {
                    fileTemplate = fileTemplate.replace(Placeholder.FIND_IS_DEL.getPlaceholder(),
                        "\n" + Templates.template.get(Templates.xmlFindIsDel).getContent());
                }
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.FIND_IS_DEL.getPlaceholder(), "");
            }
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlDeleteUpdate).getContent() + "\n");
            fileTemplate = fileTemplate.replace(Placeholder.SELECT_DEL.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlSelectDel).getContent() + "\n");
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.DELETE_UPDATE.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.SELECT_DEL.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.FIND_IS_DEL.getPlaceholder(), "");
        }
        if (ApplicationProperties.generateClear) {
            fileTemplate = fileTemplate.replace(Placeholder.CLEAR_TEMPLATE.getPlaceholder(),
                "\n" + Templates.template.get(Templates.xmlDeleteClear).getContent() + "\n");
            if (filePropertie.getExistDel()) {
                fileTemplate = fileTemplate.replace(Placeholder.CLEAR_DEL_TEMPLATE.getPlaceholder(),
                    "\n" + Templates.template.get(Templates.xmlDeleteClearDel).getContent() + "\n");
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.CLEAR_DEL_TEMPLATE.getPlaceholder(), "");
            }
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.CLEAR_TEMPLATE.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.CLEAR_DEL_TEMPLATE.getPlaceholder(), "");
        }

        return fileTemplate;
    }

    /**
     * 给模板替换不确定的模板占位符
     * 
     * @param fileTemplate
     * @param filePropertie
     * @return
     */
    public static String replaceBasePlaceholder(String fileTemplate, FileProperties filePropertie,
        HashSet<String> asList, int template) {
        String idTemplate = null;
        String serialNumberTemplate = null;
        String delTemplate = null;
        String idDelTemplate = null;
        String serialNumberDelTemplate = null;
        String clearTemplate = null;
        String clearDelTemplate = null;
        if (template == Templates.entity) {

        } else if (template == Templates.daoInterface) {
            idTemplate = Templates.template.get(Templates.daoInterfaceId).getContent();
            serialNumberTemplate = Templates.template.get(Templates.daoInterfaceSerialNumber).getContent();
            delTemplate = Templates.template.get(Templates.daoInterfaceDel).getContent();
            idDelTemplate = Templates.template.get(Templates.daoInterfaceIdDel).getContent();
            serialNumberDelTemplate = Templates.template.get(Templates.daoInterfaceSerialNumberDel).getContent();
            clearTemplate = Templates.template.get(Templates.daoInterfaceClear).getContent();
            clearDelTemplate = Templates.template.get(Templates.daoInterfaceClearDel).getContent();
        } else if (template == Templates.dao) {
            idTemplate = Templates.template.get(Templates.daoId).getContent();
            serialNumberTemplate = Templates.template.get(Templates.daoSerialNumber).getContent();
            delTemplate = Templates.template.get(Templates.daoDel).getContent();
            idDelTemplate = Templates.template.get(Templates.daoIdDel).getContent();
            serialNumberDelTemplate = Templates.template.get(Templates.daoSerialNumberDel).getContent();
            clearTemplate = Templates.template.get(Templates.daoClear).getContent();
            clearDelTemplate = Templates.template.get(Templates.daoClearDel).getContent();
        } else if (template == Templates.serviceInterface) {
            idTemplate = Templates.template.get(Templates.serviceInterfaceId).getContent();
            serialNumberTemplate = Templates.template.get(Templates.serviceInterfaceSerialNumber).getContent();
            delTemplate = Templates.template.get(Templates.serviceInterfaceDel).getContent();
            idDelTemplate = Templates.template.get(Templates.serviceInterfaceIdDel).getContent();
            serialNumberDelTemplate = Templates.template.get(Templates.serviceInterfaceSerialNumberDel).getContent();
            clearTemplate = Templates.template.get(Templates.serviceInterfaceClear).getContent();
            clearDelTemplate = Templates.template.get(Templates.serviceInterfaceClearDel).getContent();
        } else if (template == Templates.service) {
            idTemplate = Templates.template.get(Templates.serviceId).getContent();
            serialNumberTemplate = Templates.template.get(Templates.serviceSerialNumber).getContent();
            delTemplate = Templates.template.get(Templates.serviceDel).getContent();
            idDelTemplate = Templates.template.get(Templates.serviceIdDel).getContent();
            serialNumberDelTemplate = Templates.template.get(Templates.serviceSerialNumberDel).getContent();
            clearTemplate = Templates.template.get(Templates.serviceClear).getContent();
            clearDelTemplate = Templates.template.get(Templates.serviceClearDel).getContent();
        } else if (template == Templates.controller) {
            if (filePropertie.getExistId() && ApplicationProperties.isGenerateid()
                || filePropertie.getExistSerialNumber() && ApplicationProperties.isGenerateserialnumber()) {
                asList.add(JavaEquals.PATH_VARIABLE.getPackageName());
            }
            idTemplate = Templates.template.get(Templates.controllerId).getContent();
            serialNumberTemplate = Templates.template.get(Templates.controllerSerialNumber).getContent();
            delTemplate = Templates.template.get(Templates.controllerDel).getContent();
            idDelTemplate = Templates.template.get(Templates.controllerIdDel).getContent();
            serialNumberDelTemplate = Templates.template.get(Templates.controllerSerialNumberDel).getContent();
            clearTemplate = Templates.template.get(Templates.controllerClear).getContent();
            clearDelTemplate = Templates.template.get(Templates.controllerClearDel).getContent();
        } else if (template == Templates.xml) {

        } else if (template == Templates.sql) {
            idTemplate = Templates.template.get(Templates.controllerId).getContent();
            serialNumberTemplate = Templates.template.get(Templates.controllerSerialNumber).getContent();
            delTemplate = Templates.template.get(Templates.controllerDel).getContent();
            idDelTemplate = Templates.template.get(Templates.controllerIdDel).getContent();
            serialNumberDelTemplate = Templates.template.get(Templates.controllerSerialNumberDel).getContent();
        }
        idTemplate = "\n" + idTemplate + "\n";
        serialNumberTemplate = "\n" + serialNumberTemplate + "\n";
        delTemplate = "\n" + delTemplate + "\n";
        idDelTemplate = "\n" + idDelTemplate + "\n";
        serialNumberDelTemplate = "\n" + serialNumberDelTemplate + "\n";
        clearTemplate = "\n" + clearTemplate + "\n";
        clearDelTemplate = "\n" + clearDelTemplate + "\n";

        // 之前是为了添加List包
        // if (filePropertie.getExistId() && ApplicationProperties.isGenerateid()
        // || filePropertie.getExistSerialNumber() && ApplicationProperties.isGenerateserialnumber()
        // || filePropertie.getExistDel() && ApplicationProperties.isGenerateisdel()) {
        // asList.add(JavaEquals.LIST.getPackageName());
        // }
        if (filePropertie.getExistId() && ApplicationProperties.isGenerateid()) {
            fileTemplate = fileTemplate.replace(Placeholder.ID_TEMPLATE.getPlaceholder(), idTemplate);
            if (filePropertie.getExistDel() && ApplicationProperties.isGenerateisdel()) {
                fileTemplate = fileTemplate.replace(Placeholder.ID_DEL_TEMPLATE.getPlaceholder(), idDelTemplate);
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.ID_DEL_TEMPLATE.getPlaceholder(), "");
            }
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.ID_TEMPLATE.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.ID_DEL_TEMPLATE.getPlaceholder(), "");
        }
        if (filePropertie.getExistSerialNumber() && ApplicationProperties.isGenerateserialnumber()) {
            fileTemplate =
                fileTemplate.replace(Placeholder.SERIAL_NUMBER_TEMPLATE.getPlaceholder(), serialNumberTemplate);
            if (filePropertie.getExistDel() && ApplicationProperties.isGenerateisdel()) {
                fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_DEL_TEMPLATE.getPlaceholder(),
                    serialNumberDelTemplate);
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_DEL_TEMPLATE.getPlaceholder(), "");
            }
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_TEMPLATE.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.SERIAL_NUMBER_DEL_TEMPLATE.getPlaceholder(), "");
        }
        if (filePropertie.getExistDel() && ApplicationProperties.isGenerateisdel()) {
            fileTemplate = fileTemplate.replace(Placeholder.DEL_TEMPLATE.getPlaceholder(), delTemplate);
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.DEL_TEMPLATE.getPlaceholder(), "");
        }
        if (ApplicationProperties.generateClear) {
            fileTemplate = fileTemplate.replace(Placeholder.CLEAR_TEMPLATE.getPlaceholder(), clearTemplate);
            if (filePropertie.getExistDel()) {
                fileTemplate = fileTemplate.replace(Placeholder.CLEAR_DEL_TEMPLATE.getPlaceholder(), clearDelTemplate);
            } else {
                fileTemplate = fileTemplate.replace(Placeholder.CLEAR_DEL_TEMPLATE.getPlaceholder(), "");
            }
        } else {
            fileTemplate = fileTemplate.replace(Placeholder.CLEAR_TEMPLATE.getPlaceholder(), "");
            fileTemplate = fileTemplate.replace(Placeholder.CLEAR_DEL_TEMPLATE.getPlaceholder(), "");
        }

        return fileTemplate;
    }

    /**
     * 给模板替换属性占位符
     * 
     * @param fileTemplate
     * @param filePropertie
     * @return
     */
    public static String replacePropertyPlaceholder(String fieldTemplate, FileFeild fileFeild) {
        String fieldComment = fileFeild.getFieldComment();
        if (fieldComment == null) {
            fieldComment = "";
        }

        fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_COMMENT.getPlaceholder(), fieldComment);
        fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_TYPE.getPlaceholder(), fileFeild.getFieldJavaType());
        fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_NAME_D.getPlaceholder(), fileFeild.getFieldNameD());
        fieldTemplate = fieldTemplate.replace(Placeholder.FIELD_NAME_X.getPlaceholder(), fileFeild.getFieldNameX());

        return fieldTemplate;
    }

    /**
     * 将拼好的子模板替换掉主模板占位符
     * 
     * @param fileTemplate
     * @param filePropertie
     * @return
     */
    public static String replaceChildPlaceholder(String fileTemplate, FileProperties filePropertie, int template) {
        return fileTemplate;
    }

}